import React, { Component } from 'react';
class Counter extends Component {
    constructor(props) {
        super(props);
    }
    state = {
        num: this.props.value
    }
    // 方法
    setnum(val) {
        if (val < this.props.min) {
            val = this.props.min
            return
        }
        if (val > this.props.max) {
            val = this.props.max
            return
        }
        this.setState({ num: val }, () => {
            this.props.onChang(val)
        })

    }
    render() {
        return (
            <div>
                <button onClick={() => { this.setnum(this.state.num - this.props.step) }}>-</button>
                <input value={this.state.num} onChange={(e) => { this.setnum(Number(e.target.value)) }} />
                <button onClick={() => { this.setnum(this.state.num + this.props.step) }}>+</button>
            </div>
        );
    }
}

export default Counter;

// props默认值
Counter.defaultProps = {
    value: 1,
    step: 1,
    min: -Infinity,
    max: Infinity,
    // Infinity  常量：代表无穷
}